﻿using System.Data.Entity;
using System.Data.EntityModel;
using DomusDotNet.Sbm.ObjectModel;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.ModelConfiguration.Configuration;
using DomusDotNet.Sbm.ObjectModel.Configurations;
using System.Data.Objects;

namespace DomusDotNet.Sbm.ObjectModel.DbModel
{
    public class Db : DbContext
    {
        public DbSet<Book> Books { get; set; }
        public DbSet<Loan> Loans { get; set; }
        public DbSet<Author> Authors { get; set; }
        public DbSet<Editor> Editors { get; set; }
        public DbSet<Member> Members { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            ////Configure Entities.
            modelBuilder.Configurations.Add(new BookConfiguration());
            modelBuilder.Configurations.Add(new MemberConfiguration());
            modelBuilder.Configurations.Add(new AddressConfiguration());

            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        }

        /// <summary>
        /// Get Underline ObjectContext.
        /// </summary>
        public System.Data.Objects.ObjectContext CurrentObjectContext
        {
            get { return (this as System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext; }
        }

    }
}